<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\User;
use App\Models\Comment;
use App\Events\UserScoreUpdateEvent;


class Post extends Model {
    use SoftDeletes;

    const PUBLIC_STATUS = 1;

    protected $dates = ['deleted_at'];


    public function user() {
        return $this->hasOne('App\Models\User', 'id', 'uid');
    }

    public function luo() {
        return $this->hasOne('App\Models\Luo', 'id', 'luo_id');
    }

    public function reviewer() {
        return $this->hasOne('App\Models\AdminUser', 'id', 'reviewer_id');
    }

    public function comments() {
        return $this->hasMany('App\Models\Comment', 'post_id', 'id');
    }

    public function add_comment($content, User $user) {
        $comment = new Comment;
        $comment->uid = $user->id;
        $comment->post_id = $this->id;
        $comment->content = $content;
        $comment->save();
        event(new UserScoreUpdateEvent($user, UserScoreUpdateEvent::USER_ADD_COMMENT_EVENT));
        return $comment;
    }

    public function is_public() {
        return $this->status == Post::PUBLIC_STATUS;
    }
}
